/**
 * 
 *	翰迪数据
 * Copyright (c) 2013-2019 Handy-data,Inc.All Rights Reserved.
 */
package com.handydata.antifraud.ws;

import com.handydata.antifraud.to.WsReqTO;
import com.handydata.antifraud.to.WsResTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.MutablePair;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 
 * @author guolong
 * @version $Id: WsServiceFactory.java, v 0.1 2019年5月1日 下午11:50:50 apple Exp $
 */
@Slf4j
@Component
public class WsServiceFactory {

	private static ConcurrentHashMap<String, MutablePair<WsInvocationService<WsReqTO, WsResTO>, Class<?>>> wsServiceContainer = new ConcurrentHashMap<>();
	private static ConcurrentHashMap<String, String> wsServiceCn = new ConcurrentHashMap<>();

	public MutablePair<WsInvocationService<WsReqTO, WsResTO>, Class<?>> getWsService(String beanName) {
		String realBeanName = wsServiceCn.get(beanName);
		if(Objects.nonNull(realBeanName)) {
			beanName = realBeanName;
		}else {
			log.error("未找到外部数据源接口realBeanName:{}"+beanName);
		}
		return wsServiceContainer.get(beanName);
	}
	public void registerWsService(String beanName, MutablePair<WsInvocationService<WsReqTO, WsResTO>, Class<?>> pair) {
		String[] beanNameSplit = new String[]{beanName};
		if(beanName.contains(":")) {
			beanNameSplit = beanName.split(":");
			wsServiceCn.putIfAbsent(beanNameSplit[1], beanNameSplit[0]);
		}
		wsServiceContainer.putIfAbsent(beanNameSplit[0], pair);
	}
	public String getBeanName(String desc){
		return wsServiceCn.get(desc);
	}
}
